package com.fenfen.novel.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenfen.novel.R;
import com.fenfen.novel.model.Novel;
import com.fenfen.novel.utils.CommonUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class NovelAdapter extends RecyclerView.Adapter<NovelAdapter.NovelViewHolder> {

  private List<Novel> mData = new ArrayList<>();
  private Context mContext;

  private static int VIEW_TYPE_NOVEL = 1;
  private static int VIEW_TYPE_LOAD = 2;

  private boolean hasLoadingMore;

  private int colorAccent;

  private NovelItemClickListener mItemClickListener;

  public NovelAdapter(Context context, List<Novel> list) {
    this.mContext = context;
    setData(list);
    colorAccent = CommonUtil.resolveColor(mContext, R.attr.colorAccent);
  }

  @Override
  public int getItemViewType(int position) {
    int size = mData.size();
    if (hasLoadingMore && size != 0 && position == size) {
      return VIEW_TYPE_LOAD;
    } else {
      return VIEW_TYPE_NOVEL;
    }
  }

  @Override
  public NovelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == VIEW_TYPE_LOAD) {
      TextView tv = new TextView(mContext);
      tv.setId(R.id.loading_more);
      tv.setText(R.string.loading_more);
      int padding = mContext.getResources().getDimensionPixelSize(R.dimen.small_content_padding);
      tv.setPadding(padding, padding, padding, padding);
      //noinspection deprecation
      tv.setTextAppearance(mContext, android.R.style.TextAppearance_Small);
      tv.setTextColor(colorAccent);
      return new NovelViewHolder(tv);
    } else {
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_top_item, parent, false);
      NovelItemViewHolder holder = new NovelItemViewHolder(v);
      return holder;
    }
  }

  @Override
  public int getItemCount() {
    int novelSize = mData.size();
    if (hasLoadingMore && novelSize > 0) {
      return novelSize + 1;
    } else {
      return novelSize;
    }
  }

  @Override
  public void onBindViewHolder(NovelViewHolder holder, int position) {
    if (hasLoadingMore && position == mData.size()) {
      //done nothing
    } else {
      NovelItemViewHolder itemViewHolder = (NovelItemViewHolder) holder;
      Novel novel = mData.get(position);
      itemViewHolder.name.setText(novel.getName());
      itemViewHolder.author.setText(novel.getAuthor());
      itemViewHolder.category.setText(novel.getCategory());
      itemViewHolder.updateTime.setText(mContext.getString(R.string.novel_update_time_pre) + novel.getLastUpdateTime());
      itemViewHolder.jianjie.setText(novel.getNovelIntro());
      String coverlUrl = novel.getNovelCover();
      if (mContext != null && !TextUtils.isEmpty(coverlUrl)) {
        coverlUrl = CommonUtil.PRE_COVER_URL + coverlUrl;
        Picasso.with(mContext).load(coverlUrl).placeholder(R.drawable.nocover).into(itemViewHolder.cover);
      }
      itemViewHolder.itemView.setOnClickListener(new ItemClickListener(position));
    }
  }

  public void setHasLoadingMore(boolean hasLoadingMore) {
    this.hasLoadingMore = hasLoadingMore;
    notifyDataSetChanged();
  }

  public boolean isHasLoadingMore() {
    return hasLoadingMore;
  }

  public void clearData() {
    if (mData != null && mData.size() > 0) {
      mData.clear();
      notifyDataSetChanged();
    }
  }

  public void setData(List<Novel> list) {
    mData.clear();
    if (list != null && list.size() > 0) {
      mData.addAll(list);
    }
    notifyDataSetChanged();
  }

  public void addData(List<Novel> list) {
    if (list != null && list.size() > 0) {
      int positionStart = mData.size();
      mData.addAll(list);
//      notifyDataSetChanged();
      notifyItemRangeInserted(positionStart, list.size());
    }
  }

  public class NovelViewHolder extends RecyclerView.ViewHolder {

    public NovelViewHolder(View itemView) {
      super(itemView);
    }
  }

  public class NovelItemViewHolder extends NovelViewHolder {
    private TextView name;
    private TextView category;
    private TextView updateTime;
    private TextView author;
    private ImageView cover;
    private TextView jianjie;

    public NovelItemViewHolder(View itemView) {
      super(itemView);
      this.name = (TextView) itemView.findViewById(R.id.novel_name);
      this.category = (TextView) itemView.findViewById(R.id.novel_category);
      this.updateTime = (TextView) itemView.findViewById(R.id.novel_update_time);
      this.author = (TextView) itemView.findViewById(R.id.novel_author);
      this.cover = (ImageView) itemView.findViewById(R.id.novel_img);
      this.jianjie = (TextView) itemView.findViewById(R.id.novel_jianjie);
    }
  }

  private class ItemClickListener implements View.OnClickListener {
    private int pos;

    public ItemClickListener(int position) {
      this.pos = position;
    }

    @Override
    public void onClick(View v) {
      if (mItemClickListener != null) {
        mItemClickListener.onItemClick(v, mData.get(pos));
      }
    }
  }

  public interface NovelItemClickListener {
    void onItemClick(View v, Novel novel);
  }

  public void setItemClickListener(NovelItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
  }

}
